package com.example.hospitalManage.controller.admin.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hospitalManage.controller.admin.param.UserInfoPage;
import com.example.hospitalManage.entity.Hospital;
import com.example.hospitalManage.entity.UserInfo;
import com.example.hospitalManage.service.UserInfoService;
import com.example.hospitalManage.utils.Response;
import com.example.hospitalManage.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author 蔡超华
 * @version 1.0
 * @description
 * @date 2022/12/17 16:56
 **/
@RestController
@RequestMapping(value = "/admin/user")
@Api(value = "v1",tags = "用户模块相关接口")
@CrossOrigin
public class UserInfoManageController {
    @Autowired
    private UserInfoService userInfoService;

    //查询所有用户
    @GetMapping(value ="/users")
    @ApiOperation("查询所有用户列表")
    public ResponseResult<List<UserInfo>> getAllUser(){
        List<UserInfo> userInfoList = userInfoService.list();
        System.out.println(userInfoList);
        return Response.createOkResp(userInfoList);
    }

    @PostMapping(value ="/users/{current}/{limit}")
    @ApiOperation("查询用户分页")
    public ResponseResult getAllUserByPage(@PathVariable long current,
                                           @PathVariable long limit,
                                           @RequestBody(required = false) UserInfoPage userPage){
        //创建page对象，传递当前页，每页记录数
        Page<UserInfo> page = new Page<>(current, limit);
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        String name = userPage.getName();
        String nickname = userPage.getNickname();
        String phone = userPage.getPhone();
        String certificatesNo = userPage.getCertificatesNo();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", userPage.getName());
        }
        if (!StringUtils.isEmpty(nickname)) {
            wrapper.like("nick_name", userPage.getNickname());
        }
        if (!StringUtils.isEmpty(phone)) {
            wrapper.like("phone", userPage.getPhone());
        }
        if (!StringUtils.isEmpty(certificatesNo)) {
            wrapper.like("certificates_no", userPage.getCertificatesNo());
        }

        IPage<UserInfo> pageUserInfo = userInfoService.page(page, wrapper);
        return Response.createOkResp(pageUserInfo);
    }

    //添加注册用户
    @PostMapping(value = "/users")
    @ApiOperation("添加注册用户")
    public ResponseResult addUser(@RequestBody UserInfo userInfo){

        userInfo.setStatus(1);
        boolean save = userInfoService.save(userInfo);

//        UserInfo user = new UserInfo();
//        String openid = request.getParameter("openid");
//        String nickName= request.getParameter("nickName");
//        String phone= request.getParameter("phone");
//        String name= request.getParameter("name");
//        String certificatesType= request.getParameter("certificatesType");
//        String certificatesNo= request.getParameter("certificatesNo");
//        String certificatesUrl= request.getParameter("certificatesUrl");
//
//        user.setOpenid(openid);
//        user.setNickName(nickName);
//        user.setPhone(phone);
//        user.setName(name);
//        user.setCertificatesType(certificatesType);
//        user.setCertificatesNo(certificatesNo);
//        user.setCertificatesUrl(certificatesUrl);

        if (save) {

            return Response.createOkResp("添加成功");
        }else {
            return Response.createFailResp("添加失败");
        }

    }

    //根据姓名查询
    @GetMapping("/users/getByname/{name}")
    @ApiOperation("根据姓名查询")
    public ResponseResult<List<UserInfo>> findUserByname(@PathVariable String name){
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        List<UserInfo> userInfoList = userInfoService.list(wrapper);
        return Response.createOkResp(userInfoList);
    }


    //根据手机查询
    @GetMapping("/users/getByphone/{phone}")
    @ApiOperation("根据手机查询")
    public ResponseResult<List<UserInfo>> findUserByPhone(@PathVariable String phone){
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.like("phone", phone);
        List<UserInfo> userInfoList = userInfoService.list(wrapper);
        return Response.createOkResp(userInfoList);
    }

    @GetMapping("/users/getBynameandphon/{name}/{phone}")
    @ApiOperation("根据姓名和手机号码查询")
    public ResponseResult<List<UserInfo>> findUserBynameandphone(@PathVariable String name,@PathVariable String phone){
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        Map<String, Object> queryParamsMap = new HashMap<>();
        queryParamsMap.put("name", name);
        queryParamsMap.put("phone", phone);
        wrapper.allEq(queryParamsMap);
        List<UserInfo> userInfoList = userInfoService.list(wrapper);
        return Response.createOkResp(userInfoList);
    }

    //根据id获取信息
    @GetMapping("/users/getUserSet/{id}")
    @ApiOperation("根据id获取信息")
    public ResponseResult getHospSet(@PathVariable Long id) {
        UserInfo userInfo = userInfoService.getById(id);
        return Response.createOkResp(userInfo);
    }

    //更新
    @PostMapping("/users/updateUser")
    @ApiOperation("更新信息")
    public ResponseResult<UserInfo> updateHospitalSet(@RequestBody UserInfo userinfo) {
        boolean flag = userInfoService.updateById(userinfo);
        if(flag) {
            return Response.createOkResp("更新成功");
        } else {
            return Response.createOkResp("更新失败");
        }
    }
    @PutMapping("/users/updateById/{id}")
    @ApiOperation("根据id更新信息")
    public ResponseResult<UserInfo> update(HttpServletRequest request , @PathVariable("id") Long id) {


        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);

        UserInfo user = userInfoService.getOne(wrapper);

        String openid = request.getParameter("openid");
        String nickName= request.getParameter("nickName");
        String phone= request.getParameter("phone");
        String name= request.getParameter("name");
        String certificatesType= request.getParameter("certificatesType");
        String certificatesNo= request.getParameter("certificatesNo");
        String certificatesUrl= request.getParameter("certificatesUrl");

        user.setOpenid(openid);
        user.setNickName(nickName);
        user.setPhone(phone);
        user.setName(name);
        user.setCertificatesType(certificatesType);
        user.setCertificatesNo(certificatesNo);
        user.setCertificatesUrl(certificatesUrl);
        try {
            userInfoService.updateById(user);
            return Response.createOkResp("更新成功");
        } catch (Exception e) {
            return Response.createFailResp("更新失败");
        }
    }

    //删除单个用户
    @DeleteMapping("/users/deleteByid/{id}")
    @ApiOperation("根据id删除用户")
    public ResponseResult deleteById(@PathVariable("id") int id) {
        try{
            userInfoService.removeById(id);
            return Response.createOkResp("删除成功");
        } catch (Exception e) {
            return Response.createOkResp("删除失败");
        }
    }


    //批量删除用户
    @DeleteMapping("/users/deleteAll")
    @ApiOperation("批量删除用户")
    public ResponseResult<UserInfo> deleteAllUser(@RequestBody List<String> Ids){
        try{
            String[] strs=Ids.toArray(new String[Ids.size()]);

            int[] ids = new int[strs.length];
            for (int i = 0; i < strs.length; i++){
                ids[i] = Integer.parseInt(strs[i]);
            }
            for (int i = 0; i < strs.length; i++){
                userInfoService.removeById(ids[i]);
            }
            return Response.createOkResp("删除成功");
        } catch (Exception e) {
            return Response.createOkResp("删除失败");
        }
    }




}
